package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import java.util.Properties;

/**
 * @description:
 * @author: lenovo
 * @time: 2024-03-06 8:32
 */
public class MyBatisConfig {
        @Bean
        public PageInterceptor getPageInterceptor(){
            PageInterceptor pageInterceptor = new PageInterceptor();
            Properties properties = new Properties();
            properties.setProperty("value","true"); //开启分页查询
            pageInterceptor.setProperties(properties);
            return pageInterceptor;
        }

        @Bean
        public SqlSessionFactoryBean getSqlSessionFactory(@Autowired DruidDataSource dataSource
                                                ,@Autowired PageInterceptor pageInterceptor){
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
            sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.domain");
            //将分页插件交于MyBatis管理
            Interceptor[] plugins = {pageInterceptor};
            sqlSessionFactoryBean.setPlugins(plugins);
            return sqlSessionFactoryBean;
        }

        @Bean
        public MapperScannerConfigurer getMapperScannerConfigurer(){
            MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
            mapperScannerConfigurer.setBasePackage("com.itheima.dao");
            mapperScannerConfigurer.setSqlSessionFactoryBeanName("getSqlSessionFactory");
            return mapperScannerConfigurer;
        }
}
